package com.xl.atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {

    private ArrayList<Account> accountList = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);
    private Account localAccount;
    //启动ATM系统，展示欢迎页面
    public void start() {
        while (true) {
            System.out.println("====欢迎您进入到了ATM系统====");
            System.out.println("1、用户登录");
            System.out.println("2、用户开户");
            System.out.println("请选择：");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    login();
                    break;
                case 2:
                    createAccount();
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }

    public void login() {
        System.out.println("====用户登录功能====");
        //判断系统中是否存在账户对象，存在才能登录，不存在直接结束登录
        if (accountList.size() == 0) {
            System.out.println("系统中没有账户，请先开户！");
            return;
        }
        while (true) {
            //输入您的卡号
            System.out.println("请输入您的卡号：");
            String cardId = scanner.next();
            //判断这个账号是否存在
            Account account = getAccountById(cardId);
            if (account == null) {
                System.out.println("该卡号不存在，请重新输入！");
            } else {
                while (true) {
                    //卡号存在，输入登录密码
                    System.out.println("请输入您的密码：");
                    String password = scanner.next();
                    if (account.getPassword().equals(password)) {
                        localAccount = account;
                        //登录成功，展示用户信息
                        System.out.println("恭喜您"+account.getUserName()+"，登录成功，您的卡号是："+account.getCardId());
                        //展示登录后的操作界面
                        showUserCommand();
                        return;
                    } else {
                        System.out.println("密码输入有误，请重新输入！");
                    }
                }
            }
        }
    }

    /**
     * 展示登录后的操作界面
     */
    private void showUserCommand() {
        while (true) {
            System.out.println("===="+localAccount.getUserName() +"您可以选择如下功能进行账户的处理====");
            System.out.println("1、查询账户");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、密码修改");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            System.out.println("请选择：");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    //查询当前账户信息
                    showLoginAccount();
                    break;
                case 2:
                    //存款
                    depositMoney();
                    break;
                case 3:
                    //取钱
                    dropMoney();
                    break;
                case 4:
                    //转账
                    transferMoney();
                    break;
                case 5:
                    updatePassword();
                    return; //跳出并结束当前方法
                case 6:
                    System.out.println(localAccount.getUserName()+"，您已退出登录！");
                    return;
                case 7:
                    if (deleteAccount()) {
                        //销户成功，回到欢迎页面
                        System.out.println("====销户成功====");
                        return;
                    }
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }

    /**
     * 密码修改
     */
    private void updatePassword() {
        System.out.println("====密码修改功能====");
        while (true) {
            //提醒用户认证当前用户密码
            System.out.println("请输入当前账户的密码：");
            String password = scanner.next();
            if (!password.equals(localAccount.getPassword())) {
                System.out.println("密码输入有误，请重新输入！");
            } else {
                while (true) {
                    System.out.println("请输入您的新密码：");
                    String newPassword = scanner.next();
                    System.out.println("请再次确认密码：");
                    String confirmPassword = scanner.next();
                    if (!newPassword.equals(confirmPassword)) {
                        System.out.println("两次密码输入不一致，请重新输入！");
                    } else {
                        localAccount.setPassword(newPassword);
                        System.out.println("密码修改成功！");
                        return;
                    }
                }
            }
        }
    }

    /**
     * 销毁当前用户操作
     */
    private boolean deleteAccount() {
        System.out.println("====注销功能====");
        System.out.println("请问您确认是要销户吗？ y/n");
        String command = scanner.next();
        if (command.equalsIgnoreCase("y")) {
            //销户
            //判断用户账户是否有钱
            if (localAccount.getMoney() > 0) {
                System.out.println("您的账户还有钱，请先取款再销户！");
                return false;
            }
            accountList.remove(localAccount);
            System.out.println("您的账户已销户！");
            return true;
        }
        System.out.println("好的，账户保留！");
        return false;
    }

    /**
     * 转账
     */
    private void transferMoney() {
        System.out.println("====转账功能====");
        if (accountList.size() < 2) {
            System.out.println("当前系统中只有你一个账户，无法为其他账户转账！");
            return;
        }
        //判断自己的账户是否有钱
        if (localAccount.getMoney() <= 0) {
            System.out.println("您的账户余额不足，请先存钱！");
            return;
        }
        while (true) {
            System.out.println("请输入您要转给的卡号：");
            String cardId = scanner.next();
            Account account = getAccountById(cardId);
            if (account == null) {
                System.out.println("该卡号不存在，请重新输入！");
            } else {
                //对方账户存在，继续让用户认证姓氏
                String userName = "*"+account.getUserName().substring(1);
                System.out.println("请您输入["+userName+"]姓氏");
                String name = scanner.next();
                if (!account.getUserName().startsWith(name)) {
                    System.out.println("对不起，您认证的姓氏有问题！");
                } else {
                    while (true) {
                        //验证成功，继续让用户输入金额
                        System.out.println("请输入您要转给的金额：");
                        double money = scanner.nextDouble();
                        if (money <= 0) {
                            System.out.println("请输入正确的金额！");
                        } else {
                            if (money <= localAccount.getLimit()) {
                                if (money <= localAccount.getMoney()) {
                                    localAccount.setMoney(localAccount.getMoney() - money);
                                    account.setMoney(account.getMoney() + money);
                                    System.out.println("转账成功，您的余额为："+localAccount.getMoney());
                                    return;
                                } else {
                                    System.out.println("您的账户余额不足，无法转出这么多钱，最多可转："+localAccount.getMoney()+"元");
                                }
                            } else {
                                System.out.println("转账金额超过账户限额，无法转账！");
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 取钱
     */
    private void dropMoney() {
        System.out.println("====取款功能====");
        if (localAccount.getMoney() < 100) {
            System.out.println("您的账户余额不足100元，无法取款！");
            return;
        }
        while (true) {
            System.out.println("请输入您要取的金额：");
            double money = scanner.nextDouble();
            if (money <= 0) {
                System.out.println("请输入正确的金额！");
            } else {
                if (localAccount.getMoney() >= money) {
                    if (money <= localAccount.getLimit()) {
                        localAccount.setMoney(localAccount.getMoney() - money);
                        System.out.println("取款成功，您的余额为："+localAccount.getMoney());
                        break;
                    } else {
                        System.out.println("您的限额为"+localAccount.getLimit()+"元，请重新输入！");
                    }
                } else {
                    System.out.println("您的余额不足，您的账户中的余额是"+localAccount.getMoney()+"，请重新输入！");
                }
            }
        }
    }

    /**
     * 存款
     */
    private void depositMoney() {
        System.out.println("====存款功能====");
        while (true) {
            System.out.println("请输入您要存入的金额：");
            double money = scanner.nextDouble();
            if (money <= 0) {
                System.out.println("请输入正确的金额！");
            } else {
                localAccount.setMoney(localAccount.getMoney() + money);
                System.out.println("存款成功，您的余额为："+localAccount.getMoney());
                break;
            }
        }
    }

    /**
     * 展示当前用户信息
     */
    private void showLoginAccount() {
        System.out.println("===当前您的账户信息如下：===");
        System.out.println("卡号："+localAccount.getCardId());
        System.out.println("户主："+localAccount.getUserName());
        System.out.println("性别："+localAccount.getSex());
        System.out.println("余额："+localAccount.getMoney());
        System.out.println("每次取现额度："+localAccount.getLimit());
    }

    /**
     * 完成用户开户功能
     */
    private void createAccount() {
        System.out.println("====用户开户功能====");
        //1、创建一个账户对象，用于封装用户的开户信息
        Account account = new Account();
        //2、需要用户输入账户名、密码、性别、账户限额
        System.out.println("请您输入您的账户名称：");
        String name = scanner.next();
        account.setUserName(name);

        while (true) {
            System.out.println("请您输入您的性别：");
            char sex = scanner.next().charAt(0);
            if (sex != '男' && sex != '女') {
                System.out.println("性别输入有误，请重新输入！");
            } else {
                account.setSex(sex);
                account.setUserName(name);
                break;
            }
        }

        while (true) {
            System.out.println("请您输入您的账户密码：");
            String password = scanner.next();
            System.out.println("请您确认您的账户密码：");
            String password2 = scanner.next();
            //判断密码是否一样
            if (!password.equals(password2)) {
                System.out.println("您输入的两次密码输入不一致，请重新输入！");
            } else {
                account.setPassword(password);
                break;
            }
        }
        //取现额度也可以判断是否为数字
        System.out.println("请您输入您的取现额度：");
        double limit = scanner.nextDouble();
        account.setLimit(limit);

        //生成一个账户ID,同时不能与其他卡号重复
        String cardId = createCardId();
        account.setCardId(cardId);

        //3、把这个账户对象存入到账户集合中
        accountList.add(account);
        System.out.println("恭喜您，"+account.getUserName()+"开户成功！您的卡号是："+account.getCardId());
    }

    /**
     * 返回一个8位数字的卡号，并且这个卡号不能与其他账号的卡号重复
     * @return 卡号
     */
    private String createCardId() {
        //定义一个String类型的变量记住8位数字作为一个卡号
        String cardId = "";
        //使用循环，循环8次，连接起来形成一个卡号
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            //随机生成0-9之间的数字
            int number = random.nextInt(10);
            //将这个数字拼接起来
            cardId += number;
        }
        //判断卡号是否重复，没有重复才能作为一个新卡号返回
        if (getAccountById(cardId)==null) {
            return cardId;
        }
        return createCardId();
    }

    /**
     * 根据卡号查询账户对象返回
     * @param cardId 卡号
     * @return 账户对象
     */
    private Account getAccountById(String cardId) {
        for (Account account : accountList) {
            if (account.getCardId().equals(cardId)) {
                return account;
            }
        }
        return null;
    }
}
